<template>
	<view class="searchPage">
		<view class="searchColums">
			<image src="@/static/image/leftNovel.png" mode="" @click="handleBack"></image>
			<view class="searchInput">
				<u--input placeholder="请输入查询内容" prefixIcon="search" :autoBlur="true"
					prefixIconStyle="font-size: 22px;color: #909399" border="surround" clearable
					v-model="value" @change="change"></u--input>
			</view>
			<text @click="handleSearch">搜索</text>
		</view>
		<view class="recommandSearch" v-show="showTypeCard==0">
			<view class="recommandChild" v-for="(item,index) in recommandSearchList" :key="index"
				@click="handleRecommand(item)">
				<text>{{item.title}}</text>
			</view>
		</view>
		<view class="searchHistory" v-show="showTypeCard==0">
			<text>搜索历史</text>
			<image src="@/static/meabout.png" mode="" @click="handleClearHistory"></image>
		</view>
		<view class="historySearchList" v-show="showTypeCard==0">
			<view class="historySearchChild" v-for="(item,index) in searchHistoryList" :key="index"
				@click="handleHistory(item)">
				<text>{{item.content}}</text>
			</view>
		</view>
		<view class="condition" v-show="showTypeCard==1">
			<view class="btn" @click="handleFilter">
				<image src="@/static/meabout.png" mode=""></image>
				<text>筛选</text>
			</view>
		</view>
		<view class="findNovels" v-show="showTypeCard==1">
			<view class="novelCard" v-for="(item,index) in novelList" :key="item.novel_id">
				<image :src="item.pic?item.pic:'../../static/error.jpg'" mode=""></image>
				<view class="novelInfo">
					<view class="infoNav">
						<text>{{item.title}}</text>
					</view>
					<view class="tag">
						<text class="dot">{{item.author}}</text>
						<text class="dot">{{item.classify_name}}</text>
						<text class="dot">{{item.position==0?'连载中':'完结'}}</text>
						<text>{{item.word}}字</text>
					</view>
					<view class="describe">
						{{item.content}}
					</view>
				</view>
			</view>
		</view>
		<view class="findKey" v-show="showTypeCard==2">
			<view class="searchKeyNovel" v-for="(item,index) in dimNovelList" :key="item.novel_id"
				@click="handleKey(item)">
				<image src="@/static/image/serachIcon.png" mode=""></image>
				<text>{{item.title}}</text>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		app_getSearchhistory,
		app_getSearchNovelName,
		app_getSearchFindNovels
	} from "@/api/search/index.js"
	export default {
		data() {
			return {
				value: "",
				recommandSearchList: [{
						id: 1,
						novel_id: "b15f9f25d67c4c538807c34abc778d6c",
						title: "大夏第一道君",
					},
					{
						id: 2,
						novel_id: "a5d61bd6b22e47609e57ca092afa5265",
						title: "绝代龙医",
					},
				],
				dimNovelList: [],
				searchHistoryList: [],
				novelList: [],
				showTypeCard: 0,
				classify_id: "",
				novel_id: "",
				pageNum: 1, //页码
				pageSize: 10, //一个查询几个
				isLoading: false,
				dimPageNum: 1, //模糊查询页码
				dimPageSize: 9, //模糊查询分页
				isInputLoading: false, //输入框调用接口防抖
				inputTimer:null,//延时调用
			};
		},
		onLoad(option) {
			this.value = option.classify_name
			this.classify_id = option.classify_id
			if (this.value == "" || this.value == undefined || this.value == null) {
				return
			}
			this.getSearchFindNovels(true)

		},
		onReachBottom() {
			if (!this.novel_id && !this.classify_id) {
				this.dimPageNum += 1
				app_getSearchNovelName({
					content: this.value,
					pageNum: this.dimPageNum,
					pageSize: this.dimPageSize
				}).then((res) => {
					// console.log(res.data)
					if (res.data.length <= 0) {
						uni.showToast({
							icon: "none",
							title: "模糊查询没了呀~"
						})
						return
					}
					this.novelList = this.novelList.concat(res.data)
				}).catch((err) => {
					this.novelList = []
				})
			} else {
				this.getSearchFindNovels()
			}
		},
		created() {
			this.getUserSearchHistory()
		},
		methods: {
			handleHistory(item) {
				this.value = item.content
				this.getSearchNovelName()
			},
			handleRecommand(item) {
				this.value = item.title
				this.classify_id = ""
				this.novel_id = item.novel_id
				this.getSearchFindNovels(true)
			},
			//获取搜索历史记录
			getUserSearchHistory() {
				app_getSearchhistory().then((res) => {
					this.searchHistoryList = res.data
				}).catch((err) => {
					this.searchHistoryList = []
				})
			},
			getSearchNovelName() {
				this.novel_id = ""
				this.classify_id = ""
				this.showTypeCard = 2
				app_getSearchNovelName({
					content: this.value,
					pageNum: this.dimPageNum,
					pageSize: this.dimPageSize
				}).then((res) => {
					this.isInputLoading = false
					if (res.data.length <= 0) {
						this.novelList = []
					}
					this.dimNovelList = res.data
				}).catch((err) => {
					this.isInputLoading = false
					this.dimNovelList = []
					this.novelList = []
				})
			},
			getSearchFindNovels(val = false) {

				if (this.isLoading) {
					uni.showToast({
						icon: "none",
						title: "正在加载中~"
					})
					return
				}
				this.isLoading = true
				this.showTypeCard = 1
				if (!val) {
					this.pageNum += 1
				} else {
					this.novelList = []
				}
				app_getSearchFindNovels({
					pageNum: this.pageNum,
					pageSize: this.pageSize,
					novel_id: this.novel_id,
					category: this.classify_id
				}).then((res) => {
					if (res.data.length <= 0) {
						uni.showToast({
							icon: "none",
							title: "没有更多小说了呀~"
						})
					}
					this.isLoading = false
					// console.log(res.data)
					this.novelList = this.novelList.concat(res.data)
					//检测重复novel_id，数据库数据有问题，导致v-for，key重复报错！
					// let seen = {};
					// let duplicates = [];

					// for (let i = 0; i < this.novelList.length; i++) {
					// 	let obj = this.novelList[i];
					// 	let novel_id = obj.novel_id;

					// 	if (seen[novel_id]) {
					// 		// 如果已经看到过这个 novel_id 值，将其加入 duplicates 数组  
					// 		duplicates.push({
					// 			novel_id: obj.novel_id
					// 		});
					// 	} else {
					// 		// 否则，将这个 novel_id 值标记为已见过  
					// 		seen[novel_id] = true;
					// 	}
					// }
					// console.log(duplicates,this.pageNum)
				}).catch((err) => {
					this.isLoading = false
					this.novelList = []
				})
			},
			//清空搜索历史记录
			handleClearHistory() {

			},
			handleKey(item) {
				this.showTypeCard = 1
				this.value = item.title
				this.novel_id = item.novel_id
				this.classify_id = ""
				this.pageNum = 1
				this.getSearchFindNovels(true)
			},
			//搜索
			handleSearch() {
				if (this.value == "" || this.value == undefined || this.value == null) {
					uni.showToast({
						icon: "none",
						title: "请输入查找关键字呀~"
					})
					return
				}
				console.log(this.novel_id, this.classify_id)
				if (!this.novel_id && !this.classify_id) {
					this.showTypeCard = 1
					this.novelList = []
					this.dimPageNum=1
					app_getSearchNovelName({
						content: this.value,
						pageNum: this.dimPageNum,
						pageSize: this.dimPageSize
					}).then((res) => {
						console.log(res.data)
						if (res.data.length <= 0) {
							uni.showToast({
								icon: "none",
								title: "模糊查询没了呀~"
							})
							return
						}
						this.novelList = this.novelList.concat(res.data)
					}).catch((err) => {
						this.novelList = []
					})
					return
				} else {
					this.showTypeCard = 1
					this.pageNum = 1
					this.getSearchFindNovels(true)
				}
			},
			//筛选
			handleFilter() {

			},
			//输入框值发生改变
			change(e) {
				this.dimPageNum = 1
				if (e) {
					console.log(this.isInputLoading)
					if (this.isInputLoading) {
						clearTimeout(this.inputTimer)
						this.inputTimer = null
						this.isInputLoading = false
						return
					}
					this.isInputLoading = true
					this.inputTimer = setTimeout(() => {
						this.getSearchNovelName()
					},500)
				} else {
					this.showTypeCard = 0
					this.classify_id = ""
					this.novel_id = ""
					this.dimNovelList = []
				}
			},
			//返回上一个页面
			handleBack() {
				uni.navigateBack()
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #ececec;
	}

	.searchPage {
		position: relative;

	}

	.customBox {
		height: var(--status-bar-height);
		background-color: #fff;
	}

	.findKey {
		padding: 30rpx;
		box-sizing: border-box;
		background-color: #fff;

		.searchKeyNovel {
			display: flex;
			align-items: center;
			height: 100rpx;

			image {
				width: 32rpx;
				height: 32rpx;
				margin-right: 20rpx;
			}

			text {
				overflow: hidden;
				text-overflow: ellipsis;
				white-space: nowrap;
			}
		}
	}

	.findNovels {
		margin-top: 20rpx;
		padding: 30rpx;
		box-sizing: border-box;
		background-color: #fff;

		.novelCard {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-bottom: 20rpx;

			image {
				width: 180rpx;
				height: 260rpx;
			}

			.novelInfo {
				width: calc(100% - 210rpx);
				height: 200rpx;
				display: flex;
				flex-direction: column;
				color: #ccc;

				.infoNav {
					height: 60rpx;
					color: #000;
					font-size: 40rpx;
					line-height: 60rpx;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
				}

				.tag {
					height: 80rpx;
					display: flex;
					flex-wrap: nowrap;
					align-items: center;

					text:nth-child(1) {
						padding-right: 8rpx;
					}

					text:nth-child(2) {
						padding-left: 4rpx;
						padding-right: 8rpx;
					}

					text:nth-child(3) {
						padding-left: 4rpx;
						padding-right: 8rpx;
					}

					text:nth-child(4) {
						padding-left: 8rpx;
					}

					text {
						width: 120rpx;
						text-align: center;
						font-size: 26rpx;
						overflow: hidden;
						text-overflow: ellipsis;
						white-space: nowrap;
						position: relative;
					}

					.dot::after {
						content: "";
						position: absolute;
						top: calc(50% - 4rpx);
						right: 0rpx;
						width: 8rpx;
						height: 8rpx;
						border-radius: 50%;
						background-color: #ccc;

					}
				}

				.describe {
					height: 52rpx;
					display: -webkit-box;
					/* 你需要的高度 */
					overflow: hidden;
					/* 隐藏超出容器的部分 */
					font-size: 26rpx;
					line-height: 26rpx;
					-webkit-line-clamp: 2;
					/* 限制显示行数，这里设置为2行 */
					-webkit-box-orient: vertical;
				}
			}
		}
	}

	.condition {
		position: sticky;
		top: calc(var(--status-bar-height) + 100rpx);
		left: 0;
		z-index: 999;
		background-color: #fff;
		display: flex;
		justify-content: flex-end;
		align-items: center;
		padding: 0 30rpx;
		box-sizing: border-box;
		height: 100rpx;

		.btn {
			display: flex;
			align-items: center;

			image {
				width: 30rpx;
				height: 30rpx;
				margin-right: 10rpx;
			}
		}
	}

	.searchHistory {
		background-color: #fff;
		padding: 0 30rpx;
		box-sizing: border-box;
		display: flex;
		justify-content: space-between;
		align-items: center;

		text {
			font-size: 42rpx;
		}

		image {
			width: 40rpx;
			height: 40rpx;
		}
	}

	.historySearchList {
		background-color: #fff;
		padding: 0 30rpx;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: space-between;
		flex-wrap: wrap;

		.historySearchChild {
			width: 50%;
			height: 80rpx;
			display: flex;
			align-items: center;

			text {
				max-width: 300rpx;
				height: 60rpx;
				line-height: 60rpx;
				overflow: hidden;
				text-overflow: ellipsis;
				white-space: nowrap;
			}
		}
	}

	.recommandSearch {
		background-color: #fff;
		padding: 0 30rpx;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		flex-wrap: wrap;

		.recommandChild {
			width: 50%;
			height: 80rpx;
			display: flex;
			align-items: center;

			text {
				max-width: 300rpx;
				height: 60rpx;
				line-height: 60rpx;
				overflow: hidden;
				text-overflow: ellipsis;
				white-space: nowrap;
			}
		}

	}

	.searchColums {
		padding: 0 30rpx;
		padding-top: var(--status-bar-height);
		position: sticky;
		top: 0;
		left: 0;
		z-index: 999;
		height: 100rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		background-color: #fff;

		image {
			width: 40rpx;
			height: 40rpx;
		}

		.searchInput {
			width: 500rpx;

			.u-border {
				border: none;
				background-color: #e7e7e7;

			}

			.uni-input-placeholder {
				color: #838383 !important;
				overflow: hidden;
				text-overflow: ellipsis;
				white-space: nowrap;
			}
		}

		text {
			font-size: 40rpx;
		}
	}
</style>